package com.avic.rong.activity.no;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;

import com.avic.rong.MainActivity;
import com.avic.rong.MyApplication;
import com.avic.rong.R;
import com.avic.rong.bean.HomePageEvent;
import com.avic.rong.bean.MinePageEvent;
import com.example.minato.minastore.utils.AppUtils;
import com.github.lzyzsd.jsbridge.BridgeHandler;
import com.github.lzyzsd.jsbridge.BridgeWebView;
import com.github.lzyzsd.jsbridge.CallBackFunction;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ModifyPhoneActivity extends AppCompatActivity {


    public static String ARG_URL = "arg_url";

    @BindView(R.id.webview)
    BridgeWebView webView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_modify_phone);
        ButterKnife.bind(this);

        String url = null;
        if (getIntent() != null){
            url = getIntent().getStringExtra(ARG_URL);
        }

        if (!TextUtils.isEmpty(url)) {
            webView.loadUrl(url);
        }

        webView.registerHandler("goBack", new BridgeHandler() {
            @Override
            public void handler(String data, CallBackFunction function) {
              finish();
            }
        });

        //首页
        webView.registerHandler("homeClick", new BridgeHandler() {
            @Override
            public void handler(String data, CallBackFunction function) {
                EventBus.getDefault().postSticky(new HomePageEvent());
                Intent intent = MainActivity.queryIntent(ModifyPhoneActivity.this);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
                startActivity(intent);
            }
        });

        //搜索
        webView.registerHandler("searchClick", new BridgeHandler() {
            @Override
            public void handler(String data, CallBackFunction function) {
                startActivity(SearchIndexActivity.queryIntent(ModifyPhoneActivity.this, AppUtils.BASE_URL + "app/searchIndex?type=0", UserMoneyListActivity.ARG_FROM_MONEY_LIST));
            }
        });
        //我的融融
        webView.registerHandler("memberClick", new BridgeHandler() {
            @Override
            public void handler(String data, CallBackFunction function) {
                EventBus.getDefault().postSticky(new MinePageEvent());
                Intent intent = MainActivity.queryIntent(ModifyPhoneActivity.this);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
            }
        });
        //在线客服
        webView.registerHandler("pushVc", new BridgeHandler() {
            @Override
            public void handler(String data, CallBackFunction function) {
                data = data.replace("\\", "");
                JsonObject jsonObject = (JsonObject) new JsonParser().parse(data);
                String urlString = jsonObject.get("url").getAsString();
                if (MyApplication.getInstance().userLoginBean != null) {
                    if (urlString.contains("?")){
                        urlString = urlString + "&userIdSign=" + MyApplication.getInstance().userLoginBean.getUserId();
                    }else{
                        urlString = urlString + "?userIdSign=" + MyApplication.getInstance().userLoginBean.getUserId();
                    }
                }

                if (urlString.contains("/cservice")){//在线客服
                    String tempUrl = urlString.replace("userIdSign","userId");
                    Intent intent = new Intent();
                    intent.setData(Uri.parse(tempUrl));
                    intent.setAction(Intent.ACTION_VIEW);
                    startActivity(intent);
                }
            }
        });




    }


    public static Intent queryIntent(Context context,String url){
        Intent intent = new Intent(context,ModifyPhoneActivity.class);
        intent.putExtra(ARG_URL,url);
        return intent;
    }
}
